define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('destroy_class', function (sourceId) {
        var obj = Window.container[sourceId];
        obj = null;
        delete Window.container[sourceId];
    });
    dsBridge.register('create_Map', function (sourceId,level,projection,centerX,centerY,maxZoom,minZoom,maxX,maxY,minX,minY) {
        var options = {
            defaultExtent: {
                center: [centerX,centerY],
                maxZoom: maxZoom,
                minZoom: minZoom,
                level: level,
                extent: [minX, minY, maxX, maxY],
                projection: projection
            }
        };
        map = new egis.carto.Map(options);
        Window.container[sourceId] = map;
    });
    dsBridge.register('Map_init', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        source.init({targetId: targetId});
        source.on('mousemove', function (button, shift, screenX, screenY, mapX, mapY, handle) {
            dsBridge.call("egis.Map_mouseMoveEvent", [sourceId,screenX, screenY, mapX, mapY]);
        })
        source.on('mousedown', function (button, shift, screenX, screenY, mapX, mapY, handle) {
            dsBridge.call("egis.Map_mouseDownEvent", [sourceId,screenX, screenY, mapX, mapY]);
        })
        source.on('mouseup', function (button, shift, screenX, screenY, mapX, mapY, handle) {
            dsBridge.call("egis.Map_mouseUpEvent", [sourceId,screenX, screenY, mapX, mapY]);
        })

        source.on('drag', function (leftX, topY, rightX, bottomY) {
            dsBridge.call("egis.Map_dragEnvent", [sourceId,leftX, topY, rightX, bottomY]);

        })
        source.on('extentchanged', function (leftX, topY, rightX, bottomY) {
            dsBridge.call("egis.Map_extentChangedEvent", [sourceId,leftX, topY, rightX, bottomY]);
        })
        source.on('click', function (button, shift, screenX, screenY, mapX, mapY, handle) {

            dsBridge.call("egis.Map_clickEvent", [sourceId,screenX, screenY, mapX, mapY]);
        })
//        source.on('dbclick', function (button, shift, screenX, screenY, mapX, mapY, handle) {
//
//              dsBridge.call("dbClickEvent",[screenX, screenY, mapX, mapY]);
//        })
        source.on('resize', function (newSize, oldSize) {
            dsBridge.call("egis.Map_resizeEvent", [sourceId,newSize, oldSize]);
        })

        source.on('resolutionchanged', function (newValue, oldValue, level) {
            dsBridge.call("egis.Map_resolutionChangedEvent", [sourceId,newValue, oldValue, level]);
        })

        source.on('dbclick', function (button, shift, screenX, screenY, mapX, mapY, handle) {
            dsBridge.call("egis.Map_dbClickEnvent", [sourceId,screenX, screenY, mapX, mapY]);
        })
        source.on('resize', function (newSize, oldSize) {
            dsBridge.call("egis.Map_resizeEnvent", [sourceId,newSize, oldSize]);
        })
        source.on('resolutionchanged', function (newValue, oldValue, level) {
            dsBridge.call("egis.Map_resolutionChangedEnvent", [sourceId,newValue, oldValue, level]);
        })
        source.on('resolutionchanged', function (newValue, oldValue, level) {
            //dsBridge.call("resolutionChangedEnvent",[newValue,oldValue,level]);
        })
    })

    dsBridge.register('Map_addLayer', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.addLayer(target);
    })

     dsBridge.register('Map_addScaleControl', function (sourceId, className,bar,steps,minWidth,units,left,right,top,bottom,background,borderRadius,color) {
            var source = Window.container[sourceId];
            var scaleControl = new egis.control.ScaleControl({
            className: className,
            bar: bar,
            steps: steps,
            minWidth: minWidth,
            units: units,
            left:left,
            right:right,
            top:top,
            bottom:bottom,
            background:background,
            borderRadius:borderRadius
            });//创建比例尺控件对象
            source.addControl(scaleControl);//将比例尺控件添加到地图
            setTimeout(()=>{
            document.querySelector(".ol-scale-line-inner").style.cssText="border:1px solid "+ color+";border-top:none;color:"+color
            },500)
        })

    dsBridge.register('Map_getExtent', function (sourceId) {
        var source = Window.container[sourceId];
        var extent = source.getExtent();
        return JSON.stringify(extent);
    })

    dsBridge.register('Map_removeLayer', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.removeLayer(target)
    })

    dsBridge.register('Map_setCursor', function (sourceId, cursor) {
        var source = Window.container[sourceId];
        source.setCursor(cursor);
    })

    dsBridge.register('Map_getViewSize', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getViewSize();
    })

    dsBridge.register('Map_setViewSize', function (sourceId, width, height) {
        var source = Window.container[sourceId];
        source.setViewSize([width, height]);
    })

    dsBridge.register('Map_getLayerCount', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getLayerCount();
    })

    dsBridge.register('Map_getLayer', function (sourceId, index) {
        var source = Window.container[sourceId];
        return source.getLayer(index);
    })

    dsBridge.register('Map_getLayers', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getLayers();

    })

    dsBridge.register('Map_getZoomLevel', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getZoomLevel();
    })

    dsBridge.register('Map_getResolution', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getResolution();
    })

    dsBridge.register('Map_setResolution', function (sourceId, resolution) {
        var source = Window.container[sourceId];
        source.setResolution(resolution);
    })

    dsBridge.register('Map_getOrigin', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getOrigin();
    })

    dsBridge.register('Map_findLayer', function (sourceId, name) {
        var source = Window.container[sourceId];
        return source.findLayer(name);
    })

    dsBridge.register('Map_moveLayer', function (sourceId, targetId, zIndex) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.moveLayer(target, zIndex);

    })

    /*dsBridge.register('Map_on', function (sourceId) {

    })

    dsBridge.register('Map_un', function (sourceId) {

    })*/

    dsBridge.register('Map_fullExtent', function (sourceId) {
        var source = Window.container[sourceId];
        source.fullExtent();
    })

    dsBridge.register('Map_zoomOut', function (sourceId) {
        var source = Window.container[sourceId];
        source.zoomOut();
    })

    dsBridge.register('Map_zoomIn', function (sourceId) {
        var source = Window.container[sourceId];
        source.zoomIn();
    })

    dsBridge.register('Map_zoomTo', function (sourceId, level) {
        var source = Window.container[sourceId];
        source.zoomTo(level);
    })

    dsBridge.register('Map_setMaxZoom', function (sourceId, maxZoom) {
        var source = Window.container[sourceId];
        source.setMaxZoom(maxZoom);
    })

    dsBridge.register('Map_getMaxZoom', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getMaxZoom();
    })

    dsBridge.register('Map_setMinZoom', function (sourceId, minZoom) {
        var source = Window.container[sourceId];
        source.setMinZoom(minZoom);
    })

    dsBridge.register('Map_getMinZoom', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getMinZoom();
    })

    dsBridge.register('Map_pan', function (sourceId, geometry, padding, constrainResolution, expandFactor) {
        var source = Window.container[sourceId];
        var target = Window.deserializer.createJsInstance(JSON.parse(geometry));
        source.pan(target);
    })

    dsBridge.register('Map_rotate', function (sourceId, angle) {
        var source = Window.container[sourceId];
        source.rotate(angle);
    })

    dsBridge.register('Map_setCenter', function (sourceId, center) {
        var source = Window.container[sourceId];
        var target = Window.deserializer.createJsInstance(JSON.parse(center));
        source.setCenter(target);
    })

    dsBridge.register('Map_setClusterScatter', function (sourceId, clusterScatter) {
        var source = Window.container[sourceId];
        source.setClusterScatter(clusterScatter);
    })

    dsBridge.register('Map_getPixelFromCoordinate', function (sourceId, coordinate) {
        var source = Window.container[sourceId];
        return source.getPixelFromCoordinate(coordinate);
    })

    dsBridge.register('Map_getCoordinateFromPixel', function (sourceId, pixel) {
        var source = Window.container[sourceId];
        return source.getCoordinateFromPixel(pixel);
    })

    dsBridge.register('Map_export', function (sourceId, name, format) {
        var source = Window.container[sourceId];
        source.export(name, format);
    })

    dsBridge.register('Map_dragPan', function (sourceId) {
        var source = Window.container[sourceId];
        source.dragPan();
    })

    dsBridge.register('Map_exportPDF', function (sourceId, name) {
        var source = Window.container[sourceId];
        source.exportPDF(name);
    })

    dsBridge.register('Map_stopDragPan', function (sourceId) {
        var source = Window.container[sourceId];
        source.stopDragPan();
    })

    dsBridge.register('Map_resumeDragPan', function (sourceId) {
        var source = Window.container[sourceId];
        source.resumeDragPan();
    })

    dsBridge.register('Map_stopPinchZoom', function (sourceId) {
        var source = Window.container[sourceId];
        source.stopPinchZoom();
    })

    dsBridge.register('Map_resumePinchZoom', function (sourceId) {
        var source = Window.container[sourceId];
        source.resumePinchZoom();
    })

    dsBridge.register('Map_stopDbClick', function (sourceId) {
        var source = Window.container[sourceId];
        source.stopDbClick();
    })

    dsBridge.register('Map_resumeDbClick', function (sourceId) {
        var source = Window.container[sourceId];
        source.resumeDbClick();
    })

    /*dsBridge.register('Map_stopMouseWheelZoom', function (sourceId) {
        var source = Window.container[sourceId];
        source.stopMouseWheelZoom();
    })

    dsBridge.register('Map_resumeMouseWheelZoom', function (sourceId) {
        var source = Window.container[sourceId];
        source.resumeMouseWheelZoom();
    })

    dsBridge.register('Map_addControl', function (sourceId) {
        var source = Window.container[sourceId];
        source.addControl();
    })

    dsBridge.register('Map_currentTool', function (sourceId, toolId) {
        var source = Window.container[sourceId];
        var tool = Window.container[toolId]
        source.currentTool(tool);
    })

    dsBridge.register('Map_onKeyDown', function (sourceId, keyCode, shiftKey) {
        var source = Window.container[sourceId];
        source.onKeyDown(keyCode, shiftKey);
    })

    dsBridge.register('Map_onKeyUp', function (sourceId, keyCode, shiftKey) {
        var source = Window.container[sourceId];
        source.onKeyUp(keyCode, shiftKey);
    })

    dsBridge.register('Map_getMapControl', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getMapControl();
    })

    dsBridge.register('Map_onWheel', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.onWheel(target);
    })

    dsBridge.register('Map_onAddLayer', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.onAddLayer(target);
    })

    dsBridge.register('Map_onRemoveLayer', function (sourceId, targetId) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.onRemoveLayer(target);
    })

    dsBridge.register('Map_removeInteractions', function (sourceId) {
        var source = Window.container[sourceId];
        source.removeInteractions();
    })

    dsBridge.register('Map_resumeInteractions', function (sourceId) {
        var source = Window.container[sourceId];
        source.resumeInteractions();
    })

    dsBridge.register('Map_setClusterScatter', function (sourceId, flag) {
        var source = Window.container[sourceId];
        source.setClusterScatter(flag);
    })

    dsBridge.register('Map_getSrid', function (sourceId) {
        var source = Window.container[sourceId];
        return source.getSrid();
    })

    dsBridge.register('Map_addScaleLineControl', function (sourceId, className) {
        var source = Window.container[sourceId];
        source.addScaleLineControl(className);
    })*/
})